<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
use Common\Model\NewsRewardModel;
class UserController extends AdminbaseController{
	protected $users_model,$role_model;
	
	function _initialize() {
		parent::_initialize();
		$this->users_model = D("Common/AdminUser");
//		$this->role_model = D("Common/Role");
		$this->area_city = D("Common/AreaCity");
	}
	function index(){
		$count=$this->users_model->where(array("user_type"=>1))->count();
		$page = $this->page($count, 20);
		$users = $this->users_model
		->where(array("user_type"=>1))
		->order("create_time DESC")
		->limit($page->firstRow . ',' . $page->listRows)
		->select();

//		$roles_src=$this->role_model->select();
//		$roles=array();
//		foreach ($roles_src as $r){
//			$roleid=$r['id'];
//			$roles["$roleid"]=$r;
//		}
		$this->assign("page", $page->show('Admin'));
//		$this->assign("roles",$roles);
		$this->assign("users",$users);
		$this->display();
	}
	
	
	function add(){
		$roles=$this->role_model->where("status=1")->order("id desc")->select();
		$this->assign("roles",$roles);
		$this->display();
	}
	
	function add_post(){
		if(IS_POST){
			if(!empty($_POST['role_id']) && is_array($_POST['role_id'])){
				$role_ids=$_POST['role_id'];
				unset($_POST['role_id']);
				if ($this->users_model->create()) {
					$result=$this->users_model->add();
					if ($result!==false) {
						$role_user_model=M("RoleUser");
						foreach ($role_ids as $role_id){
							$role_user_model->add(array("role_id"=>$role_id,"user_id"=>$result));
						}
						$this->success("添加成功！", U("user/index"));
					} else {
						$this->error("添加失败！");
					}
				} else {
					$this->error($this->users_model->getError());
				}
			}else{
				$this->error("请为此用户指定角色！");
			}
			
		}
	}
	
	
	function edit(){
		$id= intval(I("get.id"));
//		$roles=$this->role_model->where("status=1")->order("id desc")->select();
//		$this->assign("roles",$roles);
//		$role_user_model=M("RoleUser");
//		$role_ids=$role_user_model->where(array("user_id"=>$id))->getField("role_id",true);
//		$this->assign("role_ids",$role_ids);
			
		$user=$this->users_model->where(array("admin_id"=>$id))->find();
		$this->assign($user);
		$this->display();
	}
	
	function edit_post(){
		if (IS_POST) {
			if(!empty($_POST['role_id']) && is_array($_POST['role_id'])){
				if(empty($_POST['user_pass'])){
					unset($_POST['user_pass']);
				}
				$role_ids=$_POST['role_id'];
				unset($_POST['role_id']);
				if ($this->users_model->create()) {
					$result=$this->users_model->save();
					if ($result!==false) {
						$uid=intval($_POST['id']);
						$role_user_model=M("RoleUser");
						$role_user_model->where(array("user_id"=>$uid))->delete();
						foreach ($role_ids as $role_id){
							$role_user_model->add(array("role_id"=>$role_id,"user_id"=>$uid));
						}
						$this->success("保存成功！");
					} else {
						$this->error("保存失败！");
					}
				} else {
					$this->error($this->users_model->getError());
				}
			}else{
				$this->error("请为此用户指定角色！");
			}
			
		}
	}
	
	/**
	 *  删除
	 */
	function delete(){
		$id = intval(I("get.id"));
		if($id==1){
			$this->error("最高管理员不能删除！");
		}
		
		if ($this->users_model->where("id=$id")->delete()!==false) {
			M("RoleUser")->where(array("user_id"=>$id))->delete();
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	
	
	function userinfo(){
		$id=get_current_admin_id();
		$user=$this->users_model->where(array("admin_id"=>$id))->find();
		$this->assign($user);
		$this->display();
	}
	
	function userinfo_post(){
		if (IS_POST) {
			$_POST['id']=get_current_admin_id();
			$create_result=$this->users_model
			->field("username,user_email,last_login_ip,last_login_time,create_time,user_activation_key,user_status,role_id,score,user_type",true)//排除相关字段
			->create();
			if ($create_result) {
				if ($this->users_model->save()!==false) {
					$this->success("保存成功！");
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->users_model->getError());
			}
		}
	}
	
	    function ban(){
        $id=intval($_GET['id']);
    	if ($id) {
    		$rst = $this->users_model->where(array("id"=>$id,"user_type"=>1))->setField('user_status','0');
    		if ($rst) {
    			$this->success("管理员停用成功！", U("user/index"));
    		} else {
    			$this->error('管理员停用失败！');
    		}
    	} else {
    		$this->error('数据传入失败！');
    	}
    }
    
    function cancelban(){
    	$id=intval($_GET['id']);
    	if ($id) {
    		$rst = $this->users_model->where(array("id"=>$id,"user_type"=>1))->setField('user_status','1');
    		if ($rst) {
    			$this->success("管理员启用成功！", U("user/index"));
    		} else {
    			$this->error('管理员启用失败！');
    		}
    	} else {
    		$this->error('数据传入失败！');
    	}
    }
	
	function lists(){
		$usermodel = D("Common/User");
		$p=$usermodel->getpage(5);
		$list=$usermodel->order('register_time desc')->select();
		$this->list=$list;
		foreach($list as $key=>$value){
			$news_reward = new NewsRewardModel();
			$news_reward_list = $news_reward->where("user_id = ".$value['user_id'])->field("hw_money")->select();
			$list[$key]['hw_money_total'] = '0';
			foreach($news_reward_list as $reward_key=>$reward_value){
				$list[$key]['hw_money_total'] +=$reward_value['hw_money'];//number_format($reward_value['hw_money'],'2');
			}
		}
		$page=$p->show();
		$this->assign('data',$list);
		$this->assign('page',$page);
		$this->display();
	}
	
	function user_reward_list(){
		$user_id = I("user_id") ? I("user_id") : '';
		$this->assign('user_id',$user_id);
		$this->display('user_reward_lists');
	}

	function serachlists(){
		if(!empty($_POST)){
			$usermodel = D("Common/User");
			$start_time=I('param.start_time', 0);    //开始时间
			$end_time=I('param.end_time', 0);        //结束时间
			$phone=I('param.phone', 0);				 //手机号
			$nickname=I('param.nickname', 0);		 //昵称
			$sex=I('param.sex', 0);					 //性别
			$data=$usermodel->search($start_time,$end_time,$phone,$nickname,$sex);
			$value['start_time']=$start_time;
			$value['end_time']=$end_time;
			$value['phone']=$phone;
			$value['nickname']=$nickname;
			$value['sex']=$sex;
			$this->assign('value',$value);
			$this->assign('data',$data);
			$this->display('lists');
		}
	}
	
	/**
     * 状态开启
     */
    public function open($id)
    {
		$usermodel = D("Common/User");
    	if ($id > 0) {
    		$result = $usermodel->setDisableStatus($id, 1);
    		if ($result) {
    			$this->success("修改成功",U('User/lists'));
    		} else {
    			return $this->error($result['Message']);
    		}
    	} else {
    		 $this->error("参数错误！");
    	}
    }
    
    /**
     * 状态禁用
     */
    public function close($id)
    {
		$usermodel = D("Common/User");
    	if ($id > 0) {
    		$result = $usermodel->setDisableStatus($id, 0);
    		if ($result) {
    			$this->success("修改成功",U('User/lists'));
    		} else {
    			return $this->error($result['Message']);
    		}
    	} else {
    		 $this->error("参数错误！");
    	}
    }

	//用户详情
	function moremany(){
		$usermodel = D("Common/User");
		$userId = I('param.user_id', 0);
		$result=$usermodel->usermore($userId);
		$mobile_phone=$result['mobile_phone'];
		if(!empty($mobile_phone)){
			$mobile_status='1';
		}else{
			$mobile_status='0';
		}
		$province_id=$result['province_id'];
		$area_id=$result['area_id'];
		$province=$this->area_city->where(array('city_code'=>$province_id))->find();
		$area=$this->area_city->where(array('city_code'=>$area_id))->find();
		$province_name=$province['city_name'];
		$area_name=$area['city_name'];
		$this->assign('province_name',$province_name);
		$this->assign('area_name',$area_name);
		$this->assign('mobile_status',$mobile_status);
		$this->assign("data", $result);
		$this->display('detail');
	}

	//修改用户密码
	function update(){
		$usermodel = D("Common/User");
		$pwd=trim(I('post.pwd'));
		$pwd= md5(md5($pwd));
		$user_id=I('post.user_id');
		$data['password']=$pwd;
		$up=$usermodel->where(array('user_id'=>$user_id))->data($data)->save();
		if($up=='1'){
			echo '1';
		}else{
			echo '2';
		}

	}
	
}